package net.googlecode.exigenlab.task4;

import java.util.StringTokenizer;

/**
 * This class has metods, that are restructuring sentences
 */
public class RebuilderStroka {
    /**
     * This method increase first letter in a sentence
     * @param sentence
     * @return correct sentence
     */
    protected String copitalize(String sentence){
        char first = sentence.charAt(0);
        first = Character.toUpperCase(first);
        sentence = first + sentence.substring(1);
        return sentence;
    }

    /**
     * This method results in a sentence to the correct form
     * @param sentence
     * @return correct sentence
     */
    protected String nomalizing(String sentence){
        String returnsentence;
        returnsentence = sentence.toLowerCase();
        returnsentence = copitalize(returnsentence);
        StringTokenizer token = new StringTokenizer(returnsentence," \n\t");
        StringBuffer optim = new StringBuffer();
        while (token.hasMoreTokens()){
            optim = optim.append(token.nextToken()).append(" ");
        }
        returnsentence = optim.toString();
        return returnsentence;
    }

    /**
     * This metod changes in the sentence all the characters 'A' or 'a' to '!'
     * @param sentence
     * @return modificated sentence
     */
    protected String exchanging(String sentence){
        String returnsentence;
        String a = new String(sentence);
        returnsentence = sentence.replaceAll("A", "!");
        returnsentence = returnsentence.replaceAll("a", "!");
        return returnsentence;
    }

    /**
     * This method makes all the words in a sentence with a capital letter
     * @param sentence
     * @return sentence
     */
    protected String copitolizeAll(String sentence){
        String resultsentence;

        StringTokenizer token = new StringTokenizer(sentence," ");
        StringBuffer optim = new StringBuffer();
        while (token.hasMoreTokens()){
            String tokenword = token.nextToken();
            tokenword = copitalize(tokenword);
            optim = optim.append(tokenword).append(" ");
        }
        resultsentence = optim.toString();
        return resultsentence;
    }

    /**
     * After the initialization of this method words in a sentence written backwards
     * @param sentence
     * @return sentence
     */
    protected String replacing (String sentence){
        StringTokenizer word = new StringTokenizer(sentence," ");//сконструиров токениизер
        String resultsentence;
        String DDD;
        StringBuffer FFF = new StringBuffer();
        while (word.hasMoreTokens()){
            String reword = word.nextToken();//режу по словам
            StringBuilder sss = new StringBuilder(reword);
            DDD = sss.reverse().toString();
            FFF = FFF.append(DDD+" ");
        }
        resultsentence = FFF.toString();
        return resultsentence;
    }
}
